package com.onesports.intelligent.k12.polarlight.controller.organization.course;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.onesports.intelligent.k12.polarlight.common.utils.ResultUtil;
import com.onesports.intelligent.k12.polarlight.constant.ApiPathConstants;
import com.onesports.intelligent.k12.polarlight.domain.dto.common.IdDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.course.CourseScheduleDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.course.CourseScheduleQueryDTO;
import com.onesports.intelligent.k12.polarlight.domain.vo.common.CanChoiceDataVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.course.CourseScheduleDetailVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.course.CourseScheduleVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.course.PreScheduleVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.course.SaveScheduleVO;
import com.onesports.intelligent.k12.polarlight.security.SecurityUtils;
import com.onesports.intelligent.k12.polarlight.service.course.CourseScheduleService;
import com.onesports.intelligent.k12.polarlight.service.course.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author Wsy
 **/
@Api(tags = "机构端-教务管理-排课表API")
@RestController
@RequiredArgsConstructor
@RequestMapping(ApiPathConstants.COURSE_SCHEDULE)
@PreAuthorize("isAuthenticated()")
public class CourseScheduleController {

    private final CourseScheduleService courseScheduleService;
    private final CourseService courseService;


    @GetMapping("/preview")
    @ApiOperation("新增/编辑弹窗的预览")
    public ResponseEntity<List<PreScheduleVO>> preview(@Valid CourseScheduleDTO dto) {
        return ResultUtil.success(this.courseScheduleService.preview(dto));
    }

    @PostMapping("/add")
    @ApiOperation("新增保存")
    public ResponseEntity<SaveScheduleVO> add(@Valid @RequestBody CourseScheduleDTO dto) {
        return ResultUtil.success("", this.courseScheduleService.add(dto));
    }


    @PostMapping("/ensure-save")
    @ApiOperation("确认保存")
    public ResponseEntity<String> ensureSave(@Valid @RequestBody CourseScheduleDTO dto) {
        this.courseScheduleService.toSave(dto);
        return ResultUtil.success("操作成功");
    }


    @PostMapping("/edit")
    @ApiOperation("编辑保存")
    public ResponseEntity<SaveScheduleVO> edit(@Valid @RequestBody CourseScheduleDTO dto) {
        return ResultUtil.success("操作成功", this.courseScheduleService.edit(dto));
    }


    @GetMapping("/page")
    @ApiOperation("分页查询")
    public ResponseEntity<IPage<CourseScheduleVO>> page(CourseScheduleQueryDTO dto) {
        return ResultUtil.success(this.courseScheduleService.schedulePage(new Page<>(dto.getPage(), dto.getRow()), dto));
    }


    @PostMapping("/delete")
    @ApiOperation("删除")
    public ResponseEntity<String> delete(@Valid @RequestBody IdDTO dto) {
        this.courseScheduleService.delete(dto);
        return ResultUtil.success("操作成功");
    }


    @GetMapping("/schedule-preview")
    @ApiOperation("分页列表里的预览")
    @ApiImplicitParam(name = "id", value = "课表id", required = true, paramType = "query", dataType = "string")
    public ResponseEntity<List<CourseScheduleDetailVO>> schedulePreview(@NotBlank(message = "请选择课表") String id) {
        return ResultUtil.success(this.courseScheduleService.schedulePreview(id, null, null, null, null, null));
    }


    @PostMapping("/publish")
    @ApiOperation("发布")
    public ResponseEntity<String> publish(@Valid @RequestBody IdDTO dto) {
        this.courseScheduleService.publish(dto);
        return ResultUtil.success("操作成功");
    }


    @GetMapping("/course")
    @ApiOperation("课程下拉(当前登录人所属机构有设置开设时间的课程)")
    @ApiImplicitParam(name = "courseName", value = "课程名称", paramType = "query", dataType = "string")
    public ResponseEntity<List<CanChoiceDataVO>> course(@RequestParam(required = false) String courseName) {
        return ResultUtil.success(this.courseService.dropDown(courseName, SecurityUtils.getOrganizationId()));
    }
}

